<template>
	<view class="Administrators">
		<view
			class="Administrators-content ff-font-14 ff-bg-fff ff-margin-left-10 ff-margin-right-10 ff-margin-top-15 ff-border-radius-10">
			<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-font-15">
				<view class="ff-color-222 ff-font-weight-600">
					个人信息
				</view>
			</view>
			<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
				<view class="ff-color-999">姓名</view>
				<view class="ff-color-222">{{ userInfo.username }}</view>
			</view>
			<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom" v-if="userInfo.userIdCard">
				<view class="ff-color-999">身份证号</view>
				<view class="ff-color-222">{{ userInfo.userIdCard }}</view>
			</view>
			<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
				<view class="ff-color-999">手机号</view>
				<view class="ff-color-222">{{ userInfo.phone }}</view>
			</view>
			<view @click="changePassword" class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
				<view class="ff-color-999">密码</view>
				<view class="ff-align-items-one">
					<view class="ff-color-222 ff-margin-right-10">******</view>
					<view class="ff-color-blue">修改密码</view>
				</view>
			</view>
			<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
				<view class="ff-color-999">绑定微信</view>
				<view>
					<view @click="wechatClick" v-if="userInfo.wxInfo === null" class="ff-color-blue">绑定微信</view>
					<view @click="unbindWechatClick" v-else class="ff-color-999">已绑定</view>
				</view>
			</view>
		</view>
		<view v-if="(roleStatus == 2 || roleStatus == 3) && userInfo.orgStaff.certificateList.length > 0"
			class="Administrators-content ff-bg-fff ff-font-14 ff-margin-left-10 ff-margin-right-10 ff-margin-top-15 ff-border-radius-10">
			<view class="ff-align-items-one ff-flex-between ff-padding-15">
				<view class="ff-color-222 ff-font-weight-600">证书资质</view>
			</view>
			<view v-for="(item, ind) of userInfo.orgStaff.certificateList" :key="ind">
				<view v-if="item.macroId == 2 || item.macroId == 63">
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">工程师等级</view>
						<view class="ff-color-222">{{ item.macroName }}</view>
					</view>
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">证书编号</view>
						<view class="ff-color-222">{{ item.certificateNumber }}</view>
					</view>
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">注册号</view>
						<view class="ff-color-222">{{ item.registrationNumber }}</view>
					</view>
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">注册有效期开始日期</view>
						<view class="ff-color-222">{{ item.certificateStartTime }}</view>
					</view>
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">注册有效期结束日期</view>
						<view class="ff-color-222">{{ item.certificateValidTime }}</view>
					</view>
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">工程师岗位</view>
						<view class="ff-color-222">{{ item.orgStaff.position }}</view>
					</view>
				</view>
				<view v-if="item.macroId == 3 || item.macroId == 4 || item.macroId == 5">
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">资格等级</view>
						<view class="ff-color-222">{{ item.macroName }}</view>
					</view>
					<view class="ff-align-items-one ff-flex-between ff-padding-15 ff-solid-bottom">
						<view class="ff-color-999">证书编号</view>
						<view class="ff-color-222">{{ item.certificateNumber }}</view>
					</view>
				</view>
			</view>
		</view>
		<f-loading ref="loading" />
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		name: 'Personnel',
		data() {
			return {
				userInfo: {}, // 个人信息
				// 微信参数
				wechatInfo: {
					code: '',
					encryptedData: '',
					iv: '',
					signature: '',
					rawData: ''
				}
			};
		},
		onLoad() {
			this.getUserInfo(); // 获取的个人信息
			this.initProvider(); // 获取微信供应商
		},
		computed: {
			...mapState({
				roleStatus: modules => modules.infoList.roleStatus
			})
		},
		methods: {
			...mapMutations(['setUserInfo']),
			// 获取个人信息
			async getUserInfo() {
				this.$refs.loading.openLoading('加载中···'); // 打开loading动画
				const data = await this.$http.get({
					url: '/sys/user/info'
				});
				// console.log('获取的个人信息------', data);
				this.$refs.loading.closeLoading(); // 关闭loading动画
				if (data.code == 200) {
					this.userInfo = data.data;
					if (this.userInfo.userIdCard) {
						this.userInfo.userIdCard = this.userInfo.userIdCard.replace(/^(.{4})(?:\d+)(.{4})$/,
							'$1******$2');
					}
					this.setUserInfo(data.data); // 登录后获取的用户信息
				}
			},
			// 跳转到修改密码页面
			changePassword() {
				this.$pageJump.navigateTo('/pages/maintenance/public/personal/changePassword/changePassword');
			},
			// 绑定微信--------------
			initProvider() {
				const filters = ['weixin'];
				uni.getProvider({
					service: 'oauth',
					success: res => {
						if (res.provider) {
							this.hasProvider = true;
						}
					},
					fail: err => {
						// console.log('获取服务供应商失败：', err);
					}
				});
			},
			// 绑定微信
			wechatClick(data) {
				let that = this;
				if (!this.hasProvider) {
					this.$refs.uToast.show({
						title: '获取服务供应商失败，请重启小程序后再试!',
						type: 'error'
					});
					return;
				}
				this.$refs.loading.openLoading('微信绑定中···'); // 打开loading动画
				uni.login({
					provider: data,
					success: res => {
						that.wechatInfo.code = res.code;
						uni.getUserInfo({
							provider: data,
							success: (infoRes, errMsg) => {
								that.wechatInfo.encryptedData = infoRes.encryptedData;
								that.wechatInfo.iv = infoRes.iv;
								that.wechatInfo.signature = infoRes.signature;
								that.wechatInfo.rawData = infoRes.rawData;
								that.wechatBuilding();
							},
							fail: err => {
								this.$refs.uToast.show({
									title: err,
									type: 'error'
								});
							}
						});
					},
					fail: err => {
						this.$refs.uToast.show({
							title: err,
							type: 'error'
						});
					}
				});
			},
			// 绑定微信
			wechatBuilding() {
				let that = this;
				that.$http
					.post({
						url: '/sys/user/bindWX',
						data: that.wechatInfo
					})
					.then(res => {
						if (res.code == 200 && res.success) {
							that.$refs.uToast.show({
								title: '微信绑定成功',
								type: 'success'
							});
							setTimeout(() => {
								that.getUserInfo();
							}, 500);
						} else {
							that.$refs.uToast.show({
								title: res.msg,
								type: 'error'
							});
						}
						that.$refs.loading.closeLoading(); // 关闭loading动画
					});
			},
			// 解绑微信
			unbindWechatClick() {
				let that = this
				uni.showModal({
					title: '温馨提示',
					content: '请确认是否解绑微信？',
					confirmColor: '#3178FF',
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定') 
							that.$refs.loading.openLoading('微信解绑中···'); // 打开loading动画
							that.$http
								.post({
									url: '/sys/user/unboundWX'
								})
								.then(res => {
									if (res.code == 200 && res.success) {
										that.$refs.uToast.show({
											title: '解绑成功',
											type: 'success'
										});
										setTimeout(() => {
											that.getUserInfo();
										}, 500);
									} else {
										that.$refs.uToast.show({
											title: res.msg,
											type: 'error'
										});
									}
									that.$refs.loading.closeLoading(); // 关闭loading动画
								});
						}
					},
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.Administrators {}
</style>